package com.zmn.oms.admin.converter;

import com.zmn.oms.common.constant.CodeEnum;
import org.springframework.core.convert.converter.Converter;

/**
 * 常量枚举转换器
 *
 * @author hankangshuai
 * @date 2019-10-25
 */
public class CodeEnumConverter<T extends CodeEnum> implements Converter<String, T> {

    private final T[] values;

    public CodeEnumConverter(Class<T> targetType) {
        values = targetType.getEnumConstants();
    }

    @Override
    public T convert(String source) {
        for (T t : values) {
            if (t.getCode().equals(Integer.valueOf(source.trim()))) {
                return t;
            }
        }
        return null;
    }
}